home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / std / c++ / 432 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.5 KB  |  63 lines

  1. Path: chronicle.mti.sgi.com!austern
  2. From: clamage@Eng.Sun.COM (Steve Clamage)
  3. Newsgroups: comp.std.c++
  4. Subject: Re: Explicit?  What's that?
  5. Date: 21 Feb 1996 15:41:53 PST
  6. Organization: Sun Microsystems Inc.
  7. Approved: austern@isolde.mti.sgi.com
  8. Message-ID: <4gg8p5$lcq@engnews1.Eng.Sun.COM>
  9. References: <4gg7df$76c@charnel.ecst.csuchico.edu>
  10. Reply-To: clamage@Eng.Sun.COM
  11. NNTP-Posting-Host: isolde.mti.sgi.com
  12. X-Original-Date: 21 Feb 1996 23:10:29 GMT
  13. X-Auth: PGPMoose V1.1 PGP comp.std.c++
  14.     iQBVAwUBMSut2Uy4NqrwXLNJAQEcAAH8DUY2eWb8enoURRytfzeZ9lLl0qUs3bIe
  15.     jK9N3lqimJvXNXHMIFnK1TFFluaaG+A4UY6fMJPyaGzjB2ShMlgwoQ==
  16.     =A8yL
  17. Originator: austern@isolde.mti.sgi.com
  18.  
  19. In article 76c@charnel.ecst.csuchico.edu, mcelroy@ecst.CSUChico.EDU (James Robert McElroy) writes:
  20. >Hmmm...  About two years ago I suggested, in this
  21. >newsgroup, that the "explicit" keyword be used
  22. >by C++.  But my suggestion was for use with 
  23. >conversion operators rather than constructors.
  24. >If I remember correctly, the majority of people
  25. >thought it  was a stupid idea.  Interesting...
  26.  
  27. Whether "explicit" should also apply to type conversion operators
  28. has been discussed here and in the C++ committee. Some people on
  29. the committee thought that the proposal to add "explicit" for
  30. constructors also applied to conversion operators, but the
  31. proposal did not contain that wording.
  32.  
  33. Personally, I don't see much utility in "explicit" for conversion
  34. operators. If you don't want a type conversion to be implicit, don't
  35. use a conversion operator, use a named function instead. Example:
  36.     class A {
  37.         ...
  38.         int to_int(); // instead of "operator int()"
  39.         friend int to_int(const A&); // another alternative
  40.     };
  41.     A a;
  42.     int i = a; // error
  43.     int j = a.to_int(); // instead of "static_cast<int>(a)"
  44.     int k = to_int(a); // another alternative
  45.  
  46. You don't have this option with constructors, and the workarounds for
  47. the lack of "explicit" constructors are often ugly.
  48.  
  49. The only advantage I can see for an "explicit" conversion operator is
  50. that a name for a complicated type, especially one involving pointers
  51. or references, might be inconvenient and not intuitive. In that
  52. case you might prefer to be able to cast to char**& instead of thinking
  53. up a name for that type.
  54. ---
  55. Steve Clamage, stephen.clamage@eng.sun.com
  56. ---
  57. [ To submit articles: Try just posting with your newsreader.  If that fails,
  58.                       use mailto:std-c++@ncar.ucar.edu
  59.   FAQ:    http://reality.sgi.com/employees/austern_mti/std-c++/faq.html
  60.   Policy: http://reality.sgi.com/employees/austern_mti/std-c++/policy.html
  61.   Comments? mailto:std-c++-request@ncar.ucar.edu 
  62. ]
  63.